var dataAll, locationId;
$(function () {
    getAjaxReq('location/listByToken',
        {}, function (res) {
            if (res.code == 1) {
                dataAll = res.data;
                initTree(dataAll);
            }
        }
    )
    layui.use(['table'], function () {
        var table = layui.table;
        var warnTime = $("#warnTime").val();
        table.render(
            renderTable('#list', "ammeterNotReportWarn/list",
                [
                    {type: 'numbers', title: '序号'},
                    {field: 'locationName', title: '地址', sort: false, align: 'left'},
                    {field: 'curAmount', title: '最后上报度数', sort: false, align: 'center', width: "20%"},
                    {field: 'readingTime', title: '最后上报时间', sort: false, align: 'center'},
                    {field: 'warnTime', title: '超时未上报时间(分钟)', sort: true, align: 'center', width: "20%"}
                ],
                {warnTime: warnTime}
            )
        );

        $("#search").click(function () {
            locationId = $("#locationId").val();
            var warnTime = $("#warnTime").val();
            table.reload('list', {
                page: {curr: 1},
                where: {
                    locationId: locationId,
                    warnTime: warnTime
                }
            })
        });


        $("#addressSearch").click(function () {
            $("#tree_div").show(200);
        });
        $(document).click(function (event) {
            var evt = event.srcElement ? event.srcElement : event.target;
            if (evt.id != "addressSearch" && evt.id != "tree_div") {
                $("#tree_div").hide(200);
            } else {
                $("#tree_div").show(200);
            }
        });
        $("#tree_div").click(function (event) {
            event.stopPropagation();
        });

    });
})
;
$("#toExcel").click(function () {
    locationId = $("#locationId").val();
    if (isEmpty(locationId)) {
        layer.msg("请选择导出地址");
    } else {
        $("#searchForm").submit()
    }
});

function initTree(data) {
    $('#jstree_div_layer').jstree({
        'core': {'data': data, "themes": {stripes: true}},
        "plugins": ["wholerow"]
    });
    $('#jstree_div_layer').bind("select_node.jstree", function (e, obj) {
        var currentNode = obj.node;
        var parents = currentNode.parents
        var name = '';
        if (parents) {
            parents.forEach(function (e) {
                var node = $('#jstree_div_layer').jstree("get_node", e)
                var t = (node.text == undefined || node.text == null) ? '' : node.text + ' - '
                name = t + name;
            })
        }
        $("#locationId").val(currentNode.id)
        $("#addressSearch").val(name + currentNode.text);
        $("#tree_div").hide(200);
    });
    $("#addressSearch").click(function () {
        $("#tree_div").show(200);
    });
    $(document).click(function (event) {
        var evt = event.srcElement ? event.srcElement : event.target;
        if (evt.id != "addressSearch" && evt.id != "tree_div") {
            $("#tree_div").hide(200);
        } else {
            $("#tree_div").show(200);
        }
    });
    $("#tree_div").click(function (event) {
        event.stopPropagation();
    });
}

$("#emptyBtn").click(function () {
    $("#tree_div").hide(200);
    $("#jstree_div_layer").jstree('destroy');
    $("#addressSearch").val("");
    $("#locationId").val("")
    dataAll.forEach(function (e) {
        e.state = {selected: false}
    })
    initTree(dataAll)
})